package com.groupbuying.provider.controller;

import com.groupbuying.provider.constants.CommonConstants;
import com.groupbuying.provider.context.FilterContextHandler;
import com.groupbuying.provider.domain.StoreAccountDO;
import com.groupbuying.provider.dto.convert.AccountConvert;
import com.groupbuying.provider.dto.AccountDTO;
import com.groupbuying.provider.dto.forManage.LoginUserDTO;
import com.groupbuying.provider.dto.forManage.StoreRoleDO;
import com.groupbuying.provider.service.StoreAccountService;
import com.groupbuying.provider.service.StoreRoleService;
import com.groupbuying.provider.utils.PageUtils;
import com.groupbuying.provider.utils.Query;
import com.groupbuying.provider.utils.R;
import com.groupbuying.provider.utils.apiresult.ApiResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api("服务商用户中心")
@RequestMapping("/user")
@RestController
public class StoreAccountController extends BaseController {

	@Autowired
	StoreRoleService roleService;
	@Autowired
    StoreAccountService storeAccountService;



	/**
	 * 服务商登录获取用户信息
	 * @returncurrentUser
	 */
	@GetMapping("/currentUser")
	public LoginUserDTO currentUser(){
		LoginUserDTO loginUserDTO = new LoginUserDTO();
		BigInteger roleId = roleService.roleIdsByUserId(FilterContextHandler.getUserID()).get(0);
		StoreRoleDO roleDO = roleService.get(roleId);
		//StoreAccountDO storeAccountDO = storeAccountService.get(FilterContextHandler.getUserID());
		loginUserDTO.setProviderId(FilterContextHandler.getName());

		//通过服务商id获取服务商主账号
		Map searchMap = new HashMap();
		searchMap.put("providerId", FilterContextHandler.getName());
		searchMap.put("providerType","0");
		List<StoreAccountDO> storeAccountDOMainList = storeAccountService.list(searchMap);
		StoreAccountDO storeAccountDOMain = storeAccountDOMainList.get(0);
		loginUserDTO.setName("(" + roleDO.getRoleName() + ")" + storeAccountDOMain.getProviderName());
		loginUserDTO.setUsername(storeAccountDOMain.getProviderName());
		return loginUserDTO;
	}

	/**
	 * 根据用户id获取用户currentUser
	 * @param id
	 * @return
	 */
	@GetMapping("{id}")
    R get(@PathVariable("id") String id ){
		StoreAccountDO storeAccountDO = storeAccountService.get(id);
		AccountDTO accountDTO = AccountConvert.MAPPER.do2dto(storeAccountService.get(id));
		return R.ok().put("data",accountDTO);
	}

	/**
	 * 分页获取用户列表
	 * @param params
	 * @return
	 */
	@GetMapping()
	public R listByPage(@RequestParam Map<String, Object> params) {
		params.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
		String providerId = FilterContextHandler.getName();
		params.put("providerId",providerId);
		Query query = new Query(params);
		List<AccountDTO> userDTOS = AccountConvert.MAPPER.dos2dtos((storeAccountService.list(query)));
		int total = storeAccountService.count(query);
		PageUtils pageUtil = new PageUtils(userDTOS, total);
		return R.ok().put("page",pageUtil);
	}

	@PostMapping("/exits")
	@ResponseBody
	boolean exits(@RequestParam Map<String, Object> params) {
		// 存在，不通过，false
		return !storeAccountService.exits(params);
	}

	//获取首页流水登陆信息以及余额
	@PostMapping("/getUshop")
	public ApiResult getUshop(){
		return  storeAccountService.getUshop();
	}

	//获取流水表
	@PostMapping("/getFlowStore")
	public ApiResult getFlowStore(@RequestParam Map<String,String> map){
		return  storeAccountService.getFlowStore(map);
	}


}
